﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace AcServerWrapper
{
    public class Rule
    {
        public enum ComparisonTypes { StartsWith, EndsWith, Contains, Regex }
        [XmlAttribute]
        public ComparisonTypes ComparisonType { get; set; }
        [XmlAttribute]
        public string Value { get; set; }

        internal bool DoesApplyTo(string acLogLine)
        {
            // An empty line cannot apply to a rule (or should have it's own comparison type
            if (string.IsNullOrEmpty(acLogLine))
                return false;

            // Simple mapping, no need for inheritance or further abstraction here
            switch (ComparisonType)
            {
                case ComparisonTypes.StartsWith:
                    return acLogLine.StartsWith(Value);
                case ComparisonTypes.EndsWith:
                    return acLogLine.EndsWith(Value);
                case ComparisonTypes.Contains:
                    return acLogLine.Contains(Value);
                case ComparisonTypes.Regex:
                    return new System.Text.RegularExpressions.Regex(Value).Match(acLogLine).Success;
                default:
                    throw new Exception("Developer fail: Comparison Type " + ComparisonType + " not mapped in AcServerLogProcessorRule.DoesApplyTo");
            }
        }
    }
}
